package com.cd.controller;

import com.cd.common.controller.BaseController;
import com.cd.common.domain.LoginUser;
import com.cd.common.domain.ResponseResult;
import com.cd.common.domain.vo.RouterVo;
import com.cd.common.dto.UserTransferDto;
import com.cd.common.domain.SysMenu;
import com.cd.common.domain.SysUser;
import com.cd.system.service.SysMenuService;
import com.cd.system.service.SysPermissionService;
import com.cd.system.service.UserService;
import com.cd.common.util.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class LoginController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private SysMenuService menuService;

//    @GetMapping("/hello")
//    public String hello() {
//        //return "琉球拳欢迎你";
//        return userService.hello();
//    }

    /**
     * @Description 用户登录
     * @Author lqq
     * @Date 2023/11/16 17:12
     **/
    @PostMapping("/login")
    public ResponseResult login(@RequestBody UserTransferDto userTransferDto) {
        SysUser user=new SysUser();
        user.setUserName(userTransferDto.getUsername());
        user.setPassword(userTransferDto.getPassword());
        //登录
        return userService.login(user);
    }

    /**
     * @Description 用户快速登录
     * @Author lqq
     * @Date 2023/11/16 17:12
     **/
    @PostMapping("/quickLogin")
    public ResponseResult quickLogin(@RequestParam Long[] roleIds) {
        //登录
        if (null==roleIds ||roleIds.length<1){
            roleIds=new Long[]{6L};
        }
        return userService.quickLogin(roleIds);
    }

    /**
     * @Description 拟单点登录
     * @Author lqq
     * @Date 2023/11/16 17:13
     **/
    @PostMapping("/loginSSO")
    @Deprecated//将在后续进行重做 对接特定供应商使用
    public ResponseResult loginSSO(@RequestParam(value = "username") String username) {
        //单点登录
        return new ResponseResult<>(888,"未开发");
        //return userService.SSOLogin(username);
    }

    /**
     * @Description 用户登出
     * @Author lqq
     * @Date 2023/11/16 17:13
     **/
//    @RequestMapping(value = "/logout",method = RequestMethod.POST)
//    public ResponseResult logout() {
//        System.out.println("dengchu");
//        return null;
//        //return userService.logout();
//    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public ResponseResult getInfo(){
        String username= AuthUtils.currentUsername();
        //SysUser user = userService.findByUsername(username);
        LoginUser user = AuthUtils.getLoginUser();
        // 角色集合
        //Set<String> roles = permissionService.getRolePermission(user.getUser());
        // 权限集合
        //Set<String> permissions = permissionService.getMenuPermission(user.getUser());
        HashMap<String, Object> map = new HashMap<>();
        ResponseResult result = new ResponseResult(200, "获取用户信息成功", map);

        map.put("user", user.getUser());
        map.put("roles", user.getAuthorities()); //可能不需要
        map.put("permissions", user.getPermissions());
        return result;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public ResponseResult getRouters(){
        //重新构造一下菜单
        Long userId = AuthUtils.currentUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        List<RouterVo> routerVos=menuService.buildMenus(menus);
        ResponseResult result = new ResponseResult(200, "获取路由信息成功", routerVos);
        return result;
    }


}
